package handler

import (
	"encoding/json"
	"fmt"

	"github.com/gin-gonic/gin"
	"lucidity.note/app/common/bean"
	"lucidity.note/app/service"
)

func RegisterNoteTreeRouter(router *gin.Engine) {
	group := router.Group("/notetree")
	{
		group.GET("/:notebookid", getNoteTreeHandler)
		group.PUT("/rename", renameTreeNodeHandler)
		group.DELETE("/delete/:notebookid/:nodeid", deleteTreeNodeHandler)
		group.POST("/new", newTreeNode)
		group.GET("/content/:notebookid/:nodeid", getNodeFileContentHandler)
		group.POST("/new/root/:notebookid/:name/:nodetype", newRootTreeNodeHandler)
		group.PUT("/update/content", updateFileContentHandler)
	}
}

// 获取笔记树
func getNoteTreeHandler(ctx *gin.Context) {
	var notebookId = ctx.Param("notebookid")
	response := &bean.ResponseParam{}
	service := &service.NoteTreeService{}
	if data, err := service.GetNoteTreeService(notebookId); nil != err {
		response.Failure(err.Error())
	} else {
		response.Data = data
		response.Succeed("加载笔记树成功")
	}
	ctx.JSON(response.Status, response)
}

// 节点重新命名
func renameTreeNodeHandler(ctx *gin.Context) {
	response := &bean.ResponseParam{}
	rowdata, _ := ctx.GetRawData() // 从c.Request.Body读取请求数据
	var params map[string]string   // 接收 json 参数
	if err := json.Unmarshal(rowdata, &params); nil != err {
		response.Failure("解析参数出错")
	} else {
		// 获取参数
		service := &service.NoteTreeService{}
		if data, err := service.RenameTreeNodeService(params["notebookid"], params["noteid"], params["newname"]); nil != err {
			response.Failure(err.Error())
		} else {
			response.Data = data
			response.Succeed("重命名操作成功")
		}
	}
	ctx.JSON(response.Status, response)
}

// 删除节点
func deleteTreeNodeHandler(ctx *gin.Context) {
	response := &bean.ResponseParam{}
	var notebookId = ctx.Param("notebookid")
	var nodeId = ctx.Param("nodeid")
	service := &service.NoteTreeService{}
	if data, err := service.DeleteTreeNodeService(notebookId, nodeId); nil != err {
		response.Failure(err.Error())
	} else {
		response.Data = data
		response.Succeed("删除节点操作成功")
	}

	ctx.JSON(response.Status, response)
}

// 新增节点
func newTreeNode(ctx *gin.Context) {
	response := &bean.ResponseParam{}
	rowdata, _ := ctx.GetRawData() // 从c.Request.Body读取请求数据
	var params map[string]string   // 接收 json 参数
	if err := json.Unmarshal(rowdata, &params); nil != err {
		response.Failure("解析参数出错")
	} else {
		service := &service.NoteTreeService{}
		if nodes, newNode, err := service.NewTreeNode(params["notebookid"], params["parentid"], params["name"], params["types"]); nil != err {
			response.Failure(err.Error())
		} else {
			response.Data = nodes
			response.ExtData = newNode
			response.Succeed("删除节点操作成功")
		}
	}
	ctx.JSON(response.Status, response)
}

// 获取节点的内容
func getNodeFileContentHandler(ctx *gin.Context) {
	response := &bean.ResponseParam{}
	var nodeId = ctx.Param("nodeid")
	var notebookid = ctx.Param("notebookid")
	service := &service.NoteTreeService{}
	if data, err := service.GetNodeFileContentService(notebookid, nodeId); nil != err {
		response.Failure(err.Error())
	} else {
		response.Data = data
		response.Succeed("获取文件信息成功")
	}
	ctx.JSON(response.Status, response)
}

// 新增根节点
func newRootTreeNodeHandler(ctx *gin.Context) {
	response := &bean.ResponseParam{}
	var notebookId = ctx.Param("notebookid")
	var name = ctx.Param("name")
	var nodeType = ctx.Param("nodetype")
	service := &service.NoteTreeService{}
	if data, extdata, err := service.NewRootTreeNodeService(notebookId, name, nodeType); nil != err {
		response.Failure(err.Error())
	} else {
		response.Data = data
		response.ExtData = extdata
		response.Succeed(fmt.Sprintf("新增%s节点成功.", name))
	}
	ctx.JSON(response.Status, response)
}

// 保存或者更新文件内容
func updateFileContentHandler(ctx *gin.Context) {
	response := &bean.ResponseParam{}
	var updateContentParam bean.UpdateNodeContent
	if err := ctx.ShouldBind(&updateContentParam); nil != err {
		response.Failure(fmt.Sprintf("更新文件内容失败: 绑定参数失败，错误详情{%s}", err.Error()))
	} else {
		service := &service.NoteTreeService{}
		if err := service.UpdateFileContentService(&updateContentParam); nil != err {
			response.Failure(err.Error())
		} else {
			response.Succeed("文件保存成功.")
		}
	}
	ctx.JSON(response.Status, response)
}
